program define rwanda_regressions_cluster, nclass
			
	syntax varlist, [savename(string) title(string) addnote(string) indcont(varlist)]

	di "`addnote'"

qui eststo sumtable: estpost tabstat `varlist',  /*
		*/ by(rollout) stat(mean sd count) c(stat) 

	esttab sumtable using "$d_out/`savename'_summtable.tex", $tab_note1 $tab_note2 /*
		*/  title("\label{tab:`savename'}Summary statistics: `title'") /*
		*/ 	note("Standard deviations in parentheses.  Sample sizes in brackets")
	

*** DROPPING HOSPITALS
drop if factype==1
drop if treatment==2

local clinic_control="has_genout has_anc has_tb has_hiv has_inmed"
local other_control="funds_equity funds_emp funds_ins funds_pool"

foreach var in `varlist' {
	eststo no_control_`var', add(num_obs e(N)): qui xi: reg `var' i.treatment `indcont', vce(cluster facil)	
	eststo fac_control_`var', add(num_obs e(N)): qui xi: reg `var' i.treatment `indcont' i.factype private logcatch i.province, vce(cluster facil)
	eststo fac_con2_`var', add(num_obs e(N)): qui xi: reg `var' i.treatment `indcont' i.factype private logcatch i.province `clinic_control' `other_control', vce(cluster facil)
	eststo fac_conint_`var', add(num_obs e(N)): qui xi: reg `var' i.treatment*private `indcont' i.factype logcatch i.province `clinic_control' `other_control', vce(cluster facil)
	
	lab var _Itreatment_1 "Treatment"
	lab var _Ifactype_3 "Health post"
	lab var _ItreXpriva_1 "Treatment X Private"
	lab var _Iprovince_2 "Southern province"
	lab var _Iprovince_3 "Eastern province"
	lab var _Iprovince_4 "Western province"
	lab var _Iprovince_5 "Kigali City"
		
		esttab no_control_`var' fac_control_`var' fac_con2_`var' fac_conint_`var' /*
			*/ using "$d_out/`var'_reg.tex", $regtab_opts /*
			*/  title("\label{tab:`var'} $`var'_desc")  /*
			*/ note("Standard errors clustered at facility level")

	drop _I*
	

}


* COMPILING REGRESSION RESULTS
local compile_reg_foot2 "footnote("P-values are for two sided tests.  Regressions controls include: log catchment population, log per capita spending, public/private facility, whether or not facility is a health post, types of clinics available in facility, number of inpatient and overnight beds, available funding types. Standard errors clustered at facility level. `addnote'")"

preserve
	compile_results `varlist', est(fac_con2)
		
	texsave_dngo outcome cmean b se p num_obs using "$d_out/`savename'_results.tex", replace /*
		*/ align(lcccc) varlabels rowsep(0.5cm) /*
		*/ title(Treatment effects: `title') /*
		*/ headlines("") frag nofix /*
		*/ `compile_reg_foot2'
		
	save "$d_interm/`savename'_results", replace
		
restore

eststo clear

end
